package com.gitee.limaozhi.socket.lesson03.example2;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author hzlimaozhi
 * @date 2019/08/26 15:08
 */
public class Server {

    public static void main(String[] args) throws IOException {
        ExecutorService executorService = Executors.newFixedThreadPool(20);

        ServerSocket serverSocket = new ServerSocket(8000);
        System.out.println("服务器：启动\n");

        try {
            while (true) {
                Socket socket = serverSocket.accept();
                ServerHandler handler = new ServerHandler(socket, executorService);
                executorService.submit(() -> {
                    handler.start();
                });
            }
        } finally {
            serverSocket.close();
            System.out.println("服务器：关闭");
        }
    }

}
